package cn.stronglink.asset.module.sys.role.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.stronglink.asset.model.Role;
import cn.stronglink.asset.module.sys.role.service.RoleService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.exception.BusinessException;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;

@Controller
@RequestMapping(value = "sys/role")
public class RoleController extends AbstractController {
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping(value = "qryRoles")
	public ResponseEntity<ModelMap> qryRoles(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {
		List<Role> list = roleService.qryRoles(params);
		return setSuccessModelMap(modelMap, list);
	}
	
	/**
	 * 添加角色接口
	 * @param role
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@OperateLog(module = "角色管理",desc="添加角色", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> createRole(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Role role) {
		int i = roleService.getRoleByName(role.getName());
		if(i > 0){
			throw new BusinessException("角色已经存在！");
		}
		roleService.insertRole(role);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 删除角色
	 * @param id
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@OperateLog(module = "角色管理",desc="删除角色", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		Integer counts=roleService.getRoleUsers(id);
		if(counts>0){
			throw new BusinessException("角色包含用户，不能删除");
		}
		roleService.delRole(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	
	/**
	 * 更新角色信息
	 * @param role
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@OperateLog(module = "角色管理",desc="修改角色详情", type = OpType.UPDATE)
	@RequestMapping(value = "update")
	public ResponseEntity<ModelMap> update(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Role role) {
		roleService.updateRoleById(role);
		return setSuccessModelMap(modelMap, null);
	}
	
	
	
}
